﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ImportPszxToCurrentStudy.CUiImportPszToCurrentStudyDlg
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using EngineeringInternalExtension.Utilities;
using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.ImportPszxToCurrentStudy;

public class CUiImportPszToCurrentStudyDlg : TxWindow, IComponentConnector
{
  private static string REGISTRY_IMPORT_PARTS_CHECKED = "Import parts checked";
  private static string REGISTRY_IMPORT_MFGS_CHECKED = "Import mfgs checked";
  private static string REGISTRY_IMPORT_RESOURCES_CHECKED = "Import resources checked";
  private static string REGISTRY_IMPORT_ENGINEERING_DATA_CHECKED = "Import engineering data checked";
  private CApImportPszxToCurrentStudyApp m_App;
  private CUiImportPszxToCurrentStudyCmd m_cmd;
  internal CUiImportPszToCurrentStudyDlg m_TxWindowImportPszx;
  internal System.Windows.Controls.GroupBox fileLocationGroupBox;
  internal Canvas canvas1;
  internal System.Windows.Controls.TextBox m_FileLocationLabel;
  internal System.Windows.Controls.Button BrowseFilesButton;
  internal System.Windows.Controls.GroupBox selectiveImportGroupBox;
  internal System.Windows.Controls.Label label1;
  internal System.Windows.Controls.CheckBox m_CheckBoxParts;
  internal System.Windows.Controls.CheckBox m_CheckBoxResources;
  internal System.Windows.Controls.CheckBox m_CheckBoxMFGS;
  internal System.Windows.Controls.CheckBox m_CheckBoxEngineeringData;
  internal System.Windows.Controls.Button OkButton;
  internal System.Windows.Controls.Button CancelButton;
  private bool _contentLoaded;

  public CUiImportPszToCurrentStudyDlg()
  {
    this.InitializeComponent();
    this.LoadCheckedControlsStatus();
    this.init();
  }

  public TxButtonCommand Cmd
  {
    get => (TxButtonCommand) this.m_cmd;
    set => this.m_cmd = value as CUiImportPszxToCurrentStudyCmd;
  }

  private void init() => this.m_App = new CApImportPszxToCurrentStudyApp();

  private void BrowseFilesButton_Click(object sender, RoutedEventArgs e)
  {
    using (OpenFileDialog openFileDialog = new OpenFileDialog())
    {
      openFileDialog.DefaultExt = ".pszx";
      openFileDialog.Filter = "pszx files |*.pszx";
      int num = (int) openFileDialog.ShowDialog();
      this.m_FileLocationLabel.Text = openFileDialog.FileName;
    }
  }

  private void TxWindow_Loaded(object sender, RoutedEventArgs e)
  {
  }

  private void CancelButton_Click(object sender, RoutedEventArgs e) => ((Window) this).Close();

  private void OkButton_Click(object sender, RoutedEventArgs e)
  {
    TxErrorStack.Clear();
    string str = this.GetResourceManager().GetString("IMPORT_PSZX_CMD_NAME");
    if (!File.Exists(this.m_FileLocationLabel.Text))
    {
      int num1 = (int) TxMessageBox.Show(this.GetResourceManager().GetString("IMPORT_PSZX_CMD_PATH_NOT_EXISTS_ERROR"), str, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
    else
    {
      TxAddToStudyFromFileDataEx pszData = new TxAddToStudyFromFileDataEx();
      TxAddToStudyFromFileDataEx studyFromFileDataEx1 = pszData;
      bool? isChecked = this.m_CheckBoxEngineeringData.IsChecked;
      int num2;
      if (isChecked.HasValue)
      {
        isChecked = this.m_CheckBoxEngineeringData.IsChecked;
        bool flag = true;
        num2 = isChecked.GetValueOrDefault() == flag & isChecked.HasValue ? 1 : 0;
      }
      else
        num2 = 0;
      studyFromFileDataEx1.AddEngData = num2 != 0;
      TxAddToStudyFromFileDataEx studyFromFileDataEx2 = pszData;
      isChecked = this.m_CheckBoxMFGS.IsChecked;
      int num3;
      if (isChecked.HasValue)
      {
        isChecked = this.m_CheckBoxMFGS.IsChecked;
        bool flag = true;
        num3 = isChecked.GetValueOrDefault() == flag & isChecked.HasValue ? 1 : 0;
      }
      else
        num3 = 0;
      studyFromFileDataEx2.AddMfgs = num3 != 0;
      TxAddToStudyFromFileDataEx studyFromFileDataEx3 = pszData;
      isChecked = this.m_CheckBoxParts.IsChecked;
      int num4;
      if (isChecked.HasValue)
      {
        isChecked = this.m_CheckBoxParts.IsChecked;
        bool flag = true;
        num4 = isChecked.GetValueOrDefault() == flag & isChecked.HasValue ? 1 : 0;
      }
      else
        num4 = 0;
      studyFromFileDataEx3.AddParts = num4 != 0;
      TxAddToStudyFromFileDataEx studyFromFileDataEx4 = pszData;
      isChecked = this.m_CheckBoxResources.IsChecked;
      int num5;
      if (isChecked.HasValue)
      {
        isChecked = this.m_CheckBoxResources.IsChecked;
        bool flag = true;
        num5 = isChecked.GetValueOrDefault() == flag & isChecked.HasValue ? 1 : 0;
      }
      else
        num5 = 0;
      studyFromFileDataEx4.AddResources = num5 != 0;
      System.Windows.Input.Cursor overrideCursor = Mouse.OverrideCursor;
      Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
      try
      {
        this.m_App.PszxPath = this.m_FileLocationLabel.Text;
        this.m_App.ImportPszxToCurrentStudy(pszData);
      }
      catch (UserAbortException ex)
      {
      }
      catch (Exception ex)
      {
        this.m_App.PushImportErrorOnErrorStack(ex.Message);
      }
      Mouse.OverrideCursor = overrideCursor;
      ((Window) this).Close();
      TxErrorStackDlg.DisplayErrors();
      new TxCommandManager().NotifyCommandExecuted((ITxCommand) this.m_cmd);
    }
  }

  private void TxWindow_Closing(object sender, CancelEventArgs e)
  {
    this.StoreCheckedControlsStatus();
  }

  private void LoadCheckedControlsStatus()
  {
    this.m_CheckBoxParts.IsChecked = new bool?(this.GetBoolValueFromRegistry(CUiImportPszToCurrentStudyDlg.REGISTRY_IMPORT_PARTS_CHECKED, true));
    this.m_CheckBoxMFGS.IsChecked = new bool?(this.GetBoolValueFromRegistry(CUiImportPszToCurrentStudyDlg.REGISTRY_IMPORT_MFGS_CHECKED, true));
    this.m_CheckBoxResources.IsChecked = new bool?(this.GetBoolValueFromRegistry(CUiImportPszToCurrentStudyDlg.REGISTRY_IMPORT_RESOURCES_CHECKED, true));
    this.m_CheckBoxEngineeringData.IsChecked = new bool?(this.GetBoolValueFromRegistry(CUiImportPszToCurrentStudyDlg.REGISTRY_IMPORT_ENGINEERING_DATA_CHECKED, true));
  }

  public void StoreCheckedControlsStatus()
  {
    this.SetBoolValueToRegistry(CUiImportPszToCurrentStudyDlg.REGISTRY_IMPORT_PARTS_CHECKED, this.m_CheckBoxParts.IsChecked);
    this.SetBoolValueToRegistry(CUiImportPszToCurrentStudyDlg.REGISTRY_IMPORT_MFGS_CHECKED, this.m_CheckBoxMFGS.IsChecked);
    this.SetBoolValueToRegistry(CUiImportPszToCurrentStudyDlg.REGISTRY_IMPORT_RESOURCES_CHECKED, this.m_CheckBoxResources.IsChecked);
    this.SetBoolValueToRegistry(CUiImportPszToCurrentStudyDlg.REGISTRY_IMPORT_ENGINEERING_DATA_CHECKED, this.m_CheckBoxEngineeringData.IsChecked);
  }

  private bool GetBoolValueFromRegistry(string valueName, bool defaultValue)
  {
    bool result = defaultValue;
    object obj = (object) null;
    TxWindowSettings.RestoreWindowValue((TxWindow) this, valueName, ref obj);
    if (obj != null)
      bool.TryParse(obj as string, out result);
    return result;
  }

  private void SetBoolValueToRegistry(string valueName, bool? theValue)
  {
    if (!theValue.HasValue)
      return;
    TxWindowSettings.StoreWindowValue((TxWindow) this, valueName, (object) theValue);
  }

  private void m_FileLocationLabel_TextChanged(object sender, TextChangedEventArgs e)
  {
    this.UpdateOkButtonState();
  }

  private ResourceManager GetResourceManager()
  {
    return new ResourceManager("DnProcessSimulateCommands.ImportPszxToCurrentStudy.Resources.StringTable", ((object) this).GetType().Assembly);
  }

  private bool AreAllCheckBoxesUnChecked()
  {
    bool? isChecked1 = this.m_CheckBoxEngineeringData.IsChecked;
    bool flag1 = true;
    bool flag2 = isChecked1.GetValueOrDefault() == flag1 & isChecked1.HasValue;
    bool? isChecked2 = this.m_CheckBoxMFGS.IsChecked;
    bool flag3 = true;
    bool flag4 = isChecked2.GetValueOrDefault() == flag3 & isChecked2.HasValue;
    bool? isChecked3 = this.m_CheckBoxResources.IsChecked;
    bool flag5 = true;
    bool flag6 = isChecked3.GetValueOrDefault() == flag5 & isChecked3.HasValue;
    bool? isChecked4 = this.m_CheckBoxParts.IsChecked;
    bool flag7 = true;
    bool flag8 = isChecked4.GetValueOrDefault() == flag7 & isChecked4.HasValue;
    return !flag2 && !flag4 && !flag6 && !flag8;
  }

  private void UpdateOkButtonState()
  {
    if (this.AreAllCheckBoxesUnChecked() || this.m_FileLocationLabel.Text == "")
      this.OkButton.IsEnabled = false;
    else
      this.OkButton.IsEnabled = true;
  }

  private void m_CheckBoxParts_Checked(object sender, RoutedEventArgs e)
  {
    this.UpdateOkButtonState();
  }

  private void m_CheckBoxMFGS_Checked(object sender, RoutedEventArgs e)
  {
    this.UpdateOkButtonState();
  }

  private void m_CheckBoxEngineeringData_Checked(object sender, RoutedEventArgs e)
  {
    this.UpdateOkButtonState();
  }

  private void m_CheckBoxResources_Checked(object sender, RoutedEventArgs e)
  {
    this.UpdateOkButtonState();
  }

  private void m_CheckBoxParts_Unchecked(object sender, RoutedEventArgs e)
  {
    this.UpdateOkButtonState();
  }

  private void m_CheckBoxMFGS_Unchecked(object sender, RoutedEventArgs e)
  {
    this.UpdateOkButtonState();
  }

  private void m_CheckBoxResources_Unchecked(object sender, RoutedEventArgs e)
  {
    this.UpdateOkButtonState();
  }

  private void m_CheckBoxEngineeringData_Unchecked(object sender, RoutedEventArgs e)
  {
    this.UpdateOkButtonState();
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    System.Windows.Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/importpszxtocurrentstudy/cuiimportpsztocurrentstudydlg.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        this.m_TxWindowImportPszx = (CUiImportPszToCurrentStudyDlg) target;
        ((FrameworkElement) this.m_TxWindowImportPszx).Loaded += new RoutedEventHandler(this.TxWindow_Loaded);
        ((Window) this.m_TxWindowImportPszx).Closing += new CancelEventHandler(this.TxWindow_Closing);
        break;
      case 2:
        this.fileLocationGroupBox = (System.Windows.Controls.GroupBox) target;
        break;
      case 3:
        this.canvas1 = (Canvas) target;
        break;
      case 4:
        this.m_FileLocationLabel = (System.Windows.Controls.TextBox) target;
        this.m_FileLocationLabel.TextChanged += new TextChangedEventHandler(this.m_FileLocationLabel_TextChanged);
        break;
      case 5:
        this.BrowseFilesButton = (System.Windows.Controls.Button) target;
        this.BrowseFilesButton.Click += new RoutedEventHandler(this.BrowseFilesButton_Click);
        break;
      case 6:
        this.selectiveImportGroupBox = (System.Windows.Controls.GroupBox) target;
        break;
      case 7:
        this.label1 = (System.Windows.Controls.Label) target;
        break;
      case 8:
        this.m_CheckBoxParts = (System.Windows.Controls.CheckBox) target;
        this.m_CheckBoxParts.Checked += new RoutedEventHandler(this.m_CheckBoxParts_Checked);
        this.m_CheckBoxParts.Unchecked += new RoutedEventHandler(this.m_CheckBoxParts_Unchecked);
        break;
      case 9:
        this.m_CheckBoxResources = (System.Windows.Controls.CheckBox) target;
        this.m_CheckBoxResources.Checked += new RoutedEventHandler(this.m_CheckBoxResources_Checked);
        this.m_CheckBoxResources.Unchecked += new RoutedEventHandler(this.m_CheckBoxResources_Unchecked);
        break;
      case 10:
        this.m_CheckBoxMFGS = (System.Windows.Controls.CheckBox) target;
        this.m_CheckBoxMFGS.Checked += new RoutedEventHandler(this.m_CheckBoxMFGS_Checked);
        this.m_CheckBoxMFGS.Unchecked += new RoutedEventHandler(this.m_CheckBoxMFGS_Unchecked);
        break;
      case 11:
        this.m_CheckBoxEngineeringData = (System.Windows.Controls.CheckBox) target;
        this.m_CheckBoxEngineeringData.Checked += new RoutedEventHandler(this.m_CheckBoxEngineeringData_Checked);
        this.m_CheckBoxEngineeringData.Unchecked += new RoutedEventHandler(this.m_CheckBoxEngineeringData_Unchecked);
        break;
      case 12:
        this.OkButton = (System.Windows.Controls.Button) target;
        this.OkButton.Click += new RoutedEventHandler(this.OkButton_Click);
        break;
      case 13:
        this.CancelButton = (System.Windows.Controls.Button) target;
        this.CancelButton.Click += new RoutedEventHandler(this.CancelButton_Click);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
